package com.woniuxy.student.controller;

import com.woniuxy.commons.entity.EnrolmentRate;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.entity.zlz.*;
import com.woniuxy.student.service.EnrolmentService;
import com.woniuxy.student.util.StudentCommonUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: 曾令忠
 * @Description:
 * @Date: 2020/7/30 0030-下午 8:56
 */
@RestController
@RequestMapping("/echarts")
public class EchartsController {

    @Resource
    EnrolmentService enrolmentService;

    @RequestMapping("/findall")
    public ResponseEntity<EchartsDto> getAll(){
        ResponseEntity<EchartsDto> responseEntity = new ResponseEntity<>();
        EchartsDto echartsDto = new EchartsDto();
        echartsDto.setSubject("语文");
        List<ScoreDemo> scoreDemos = new ArrayList<>();

        ScoreDemo scoreDemo = new ScoreDemo();
        scoreDemo.setTestname("一模");
        scoreDemo.setSelfscore(80);
        scoreDemo.setClassscore(78);
        scoreDemo.setGradescore(76);
        scoreDemos.add(scoreDemo);

        ScoreDemo scoreDemo1 = new ScoreDemo();
        scoreDemo1.setTestname("二模");
        scoreDemo1.setSelfscore(83);
        scoreDemo1.setClassscore(79);
        scoreDemo1.setGradescore(77);
        scoreDemos.add(scoreDemo1);
        ScoreDemo scoreDemo2 = new ScoreDemo();
        scoreDemo2.setTestname("三模");
        scoreDemo2.setSelfscore(90);
        scoreDemo2.setClassscore(78);
        scoreDemo2.setGradescore(79);
        scoreDemos.add(scoreDemo2);

        ScoreDemo scoreDemo3 = new ScoreDemo();
        scoreDemo3.setTestname("四模");
        scoreDemo3.setSelfscore(88);
        scoreDemo3.setClassscore(90);
        scoreDemo3.setGradescore(86);
        scoreDemos.add(scoreDemo3);
        echartsDto.setScores(scoreDemos);
        responseEntity.setData(echartsDto);
        responseEntity.setCode(200);
        return responseEntity;
    }

    @RequestMapping("/radar")
    public ResponseEntity<RadarDto> getRadarChart(){
        ResponseEntity<RadarDto> responseEntity = new ResponseEntity<>();
        //模拟数据
        RadarDto radarDto = new RadarDto();
        radarDto.setTestname("高三上第一次月考");
        List<SubjectScore> subjectScores = new ArrayList<>();
        SubjectScore chinese = new SubjectScore();
        chinese.setName("语文");
        chinese.setTotal(150);
        chinese.setClassScore(98);
        chinese.setStudentScore(109);
        subjectScores.add(chinese);
        SubjectScore math = new SubjectScore();
        math.setName("数学");
        math.setTotal(150);
        math.setClassScore(112);
        math.setStudentScore(145);
        subjectScores.add(math);
        SubjectScore english = new SubjectScore();
        english.setName("英语");
        english.setTotal(150);
        english.setClassScore(99);
        english.setStudentScore(128);
        subjectScores.add(english);
        SubjectScore physical = new SubjectScore();
        physical.setName("物理");
        physical.setTotal(110);
        physical.setClassScore(93);
        physical.setStudentScore(76);
        subjectScores.add(physical);
        SubjectScore chemistry = new SubjectScore();
        chemistry.setName("化学");
        chemistry.setTotal(100);
        chemistry.setClassScore(80);
        chemistry.setStudentScore(80);
        subjectScores.add(chemistry);
        SubjectScore biology = new SubjectScore();
        biology.setName("生物");
        biology.setTotal(90);
        biology.setClassScore(78);
        biology.setStudentScore(83);
        subjectScores.add(biology);
        radarDto.setScores(subjectScores);
        responseEntity.setCode(200);
        responseEntity.setData(radarDto);
        return responseEntity;
    }

    @RequestMapping("/encrolment")
    public ResponseEntity<List<EnrolmentRate>> enrolmentRate(){
        ResponseEntity<List<EnrolmentRate>> rateResponseEntity = new ResponseEntity<>();
        int stuno = 1;
        try {
            List<EnrolmentRate> enrolmentRates = enrolmentService.getAllBySchoolId(stuno);
            rateResponseEntity.setCode(200);
            rateResponseEntity.setData(enrolmentRates);
        }catch (Exception e){
            rateResponseEntity.setCode(2);
        }
        return rateResponseEntity;
    }

    @RequestMapping("/selfRange")
    public ResponseEntity<RankList> selfRange(){
        ResponseEntity<RankList> rateResponseEntity = new ResponseEntity<>();
        Integer[] scores = {567,678,432,543,392,346,589,432,589,685,456,432,496,574,612,456,499,634,584,509,608,499};
        List<Integer> list = Arrays.asList(scores);
        Collections.sort(list);

        Map<Integer,Integer> ranks = new HashMap<>();
        TreeSet<Integer> rankSet = new TreeSet<>();
        TreeSet<Integer> scoreSet = new TreeSet<>();
        List<String> ranklist = new ArrayList<>();
        List<Integer> scorelist = new ArrayList<>();
        int index = 0;
        int num = 0;
        double temp = -1;
        for (int i: list) {
            index++;
            if (temp!=i){
                num = index;
            }
            temp = i;
           scoreSet.add(i);
           rankSet.add(num);
        }
        for (Integer rank : rankSet) {
            System.out.println(rank);
            double rankDouble = rank*100/list.size();
            ranklist.add(rankDouble+"%");
            System.out.println(rankDouble+"%");

        }
        scorelist.addAll(scoreSet);
        Collections.reverse(scorelist);
        RankList rankDto = new RankList();
        rankDto.setRanks(ranklist);
        rankDto.setScores(scorelist);
        rateResponseEntity.setData(rankDto);
        rateResponseEntity.setCode(200);
        return rateResponseEntity;
    }

    @GetMapping("/subjectRank")
    public ResponseEntity<List<Double>> getRank(){
        ResponseEntity<List<Double>> responseEntity = new ResponseEntity<>();
        Integer[] chiese = {100,124,98,134,101,89,116,98,105,124};
        Integer[] math = {100,124,89,134,101,101,116,98,124,124};
        Integer[] english = {99,124,98,134,89,89,116,98,105,143};
        Integer[] pcb = {235,206,267,198,201,189,205,256,205,274};
        Integer[] total = {534,578,552,600,492,468,553,550,539,665};
        List<Double> ranks = new ArrayList<>();
        double chirank = StudentCommonUtil.getRange(100,Arrays.asList(chiese));
        double mathrank = StudentCommonUtil.getRange(100,Arrays.asList(math));
        double engrank = StudentCommonUtil.getRange(99,Arrays.asList(english));
        double pcbrank = StudentCommonUtil.getRange(235,Arrays.asList(pcb));
        double totalrank = StudentCommonUtil.getRange(534,Arrays.asList(total));
        ranks.add(chirank);
        ranks.add(mathrank);
        ranks.add(engrank);
        ranks.add(pcbrank);
        ranks.add(totalrank);
        responseEntity.setCode(200);
        responseEntity.setData(ranks);
        return responseEntity;
    }
}
